<?php
/**
* 缺货列表缓存
 */

namespace application\timer;


use application\depend\Applog;
use application\depend\Dbiov;
use application\depend\PhpRedis;

class PurchaseList
{

    //缺货列表
    public function purchase_list(){
        $cached = new PhpRedis();


        //搜索 全部的  订单列表

        $order_info = 'order_info';  //表名
        $tb_product = 'product';
        $where = ''; //初始化 where
        $where .= " status = '2'
                    and ( sales_return ='' OR sales_return is NULL )
                    and alsobuy >0";
                   

        $sql = "SELECT * FROM $order_info WHERE $where GROUP BY buyer";

        $pdo = Dbiov::getInstance();  //实例化
        $presql = $pdo->prepareSql($sql);


        try{
            if($presql->execute()) {
                $purchase = $presql->fetchAll();
                
                
                if(empty($purchase)){
                    $err = [
                        'title' => '没有数据呢',
                        'sql' => $sql,
                        'table' => $order_info,
                        'time' => date('Y-m-d H:i:s'),
                        'pdo_err' => $presql->errorInfo(),
                    ];
                    PhpRedis::set('purchase','');
                    PhpRedis::set('purchase_list','');
                    return;
                }//if empty
                
                //新增  20191004
                //$purchase = $this->combine_multi_sku($purchase); //合并同一订单下的同个SKU的 采购需求数

            }//if 执行成功

        }catch(\Exception $e){
            $err = [
                'title' => '查询缺货列表错误:Exceptions',
                'sql' => $sql,
                'table' => $order_info,
                'time' => date('Y-m-d H:i:s'),
                'pdo_err' => $presql->errorInfo(),
            ];
            $pdo->closeDb();
            return;

        }//catch

        foreach($purchase as $key=>$val){
           
            $where = " status = '2'
                        and buyer='".$val['buyer']."' 
                        and ( sales_return ='' OR sales_return is NULL ) 
                        ";

//            $purchase_list = $tb_odi->where($where)->order('order_number')->order('supplier asc,sku asc')->select();
            $sql_purchase_list = "SELECT * FROM $order_info
                                    WHERE $where
                                    ORDER BY supplier asc,sku asc,order_number
               ";

            $presql_list = $pdo->prepareSql($sql_purchase_list);

            try{

                $presql_list->execute();

                $purchase_list = $presql_list->fetchAll();
                if(empty($purchase_list)){
                    $err = [
                        'title' => 'Empty Data for Purchase_list',
                        'sql' => $sql_purchase_list,
                        'table' => $order_info,
                        'time' => date('Y-m-d H:i:s'),
                        'pdo_err' => $presql_list->errorInfo(),
                    ];
//                        Applog::errorLog($err);
                    $pdo->closeDb();
                    return;
                }//if

                //新增  20191004    遍历 $purchase_list, 将每个$订单 de  sku 都合并好
                $purchase_list = $this->combine_multi_sku($purchase_list); //合并同一订单下的同个SKU的 采购需求数
                foreach($purchase_list as $k=>$v){
                    if($v['alsobuy'] <= 0){
                        unset($purchase_list[$k]);  //不显示 alsobuy = 0 的
                    }
                }//foreach

                $purchase_list = array_values($purchase_list);


            }catch(\Exception $e){
                $err = [
                    'title' => '查询缺货列表错误:Exceptions',
                    'sql' => $sql_purchase_list,
                    'table' => $order_info,
                    'time' => date('Y-m-d H:i:s'),
                    'pdo_err' => $presql_list->errorInfo(),
                ];
                $pdo->closeDb();
                return;
            }//catch

            
            foreach($purchase_list as $k=>$v){
                $where = " buyer='".$v['buyer']."' 
                            and sku='".$v['sku']."' 
                            and ( sales_return ='' OR sales_return is NULL ) 
                            and supplier='".$v['supplier']."' 
                            and status='2'
                            and alsobuy>0";

                $sql_ring_filter = "SELECT product_type,default_styles,product_location,available_stock,goods_prepare_situation FROM $tb_product WHERE id=". $v['p_id'];

                $presql_filter = $pdo->prepareSql($sql_ring_filter);

                try{
                    $presql_filter->execute();
                    $ring_filter = $presql_filter->fetchAll();

                    if(empty($ring_filter)){
                        $err = [
                            'title' => 'Ring_filter is empty',
                        ];
                        $pdo->closeDb();
                        return $err;
                    }
                }catch (\Exception $e){
                    $err = [
                        'title' => '查询缺货列表错误:Exceptions',
                        'sql' => $sql_ring_filter,
                        'table' => $tb_product,
                        'time' => date('Y-m-d H:i:s'),
                        'pdo_err' => $pdo->presqlErrInfo,
                    ];
                    $pdo->closeDb();
                    return;
                }//catch


                $purchase_list[$k]['product_location'] = $ring_filter[0]['product_location'];  //产品货位
                $purchase_list[$k]['available_stock'] = $ring_filter[0]['available_stock'];  //产品库存数
                $purchase_list[$k]['goods_prepare_situation'] = $ring_filter[0]['goods_prepare_situation'];  //备货情况


                $sql_alsobuy = "SELECT SUM(alsobuy) as alsobuy_sum FROM $order_info 
                                WHERE $where
                                ORDER BY supplier
                ";
                $pre_alsobuy = $pdo->prepareSql($sql_alsobuy);

                try{
                    $pre_alsobuy->execute();
                    $alsobuy = $pre_alsobuy->fetchAll();
                }catch (\Exception $e){
                    $err = [
                        'title' => 'Also_buy sum 错误:Exceptions',
                        'sql' => $sql_alsobuy,
                        'table' => $order_info,
                        'time' => date('Y-m-d H:i:s'),
                        'pdo_err' => $pdo->presqlErrInfo,
                    ];
                    $pdo->closeDb();
                    return;
                }//catch
                $purchase_list[$k]['count'] = $alsobuy[0]['alsobuy_sum'];//多加了 0


                $sql_product_price = "SELECT cost_price FROM $tb_product WHERE sku='".$v['sku']."' ";

                $pre_price = $pdo->prepareSql($sql_product_price);

                try{
                    $pre_price->execute();
                    $product_price = $pre_price->fetchAll();

                }catch (\Exception $e){
                    $err = [
                        'title' => '产品价格查询出错:Exceptions',
                        'sql' => $sql_product_price,
                        'table' => $tb_product,
                        'time' => date('Y-m-d H:i:s'),
                        'pdo_err' => $pdo->presqlErrInfo,
                    ];
                    $pdo->closeDb();
                    return ;
                }//catch

                $purchase_list[$k]['cost_price'] = isset($product_price[0]['cost_price']) ? $product_price[0]['cost_price']:'null';  //多加 0

            }//foreach 2

            $purchase[$key] = $purchase_list;


        }//foreach


        /*将结果放入缓存*/
        PhpRedis::set('purchase',$purchase);
        PhpRedis::set('purchase_list',$purchase_list);

    
    }//func purchase_list
}//class
